import * as crypto from "crypto"
export default {
  //转int类型
  parse_int(str: string | number) {
    if (typeof str === 'number') return str;
    if (!str) return 0;
    return parseInt(str) || 0;
  },
  //未使用
  code(status,...data:any){
    return status ? {code:200,data:data[0],message:'操作成功'} : {code:0,message:'操作失败'}
  },
  //获取当前日期
  getDate(n){
    let now = new Date();
    now.setDate(now.getDate() +n); 
    return Date.parse(now.toString())/1000;
  },
  //未使用
  unitity_code(status,...data:any){
    return  status ? {code:1,data:data[0],message:'操作成功'} : {code:0,message:'操作失败'}
  },
  //复制基础类型值
  assign_basic(new_obj,old_obj){
    for (const key in old_obj) {
      if (old_obj.hasOwnProperty(key)) {
        const element = old_obj[key];
        if((element||element==0)&&(typeof element =='number'||typeof element=='string' ||typeof element=='boolean')){
          new_obj[key]=element;
        }
      }
    }
    return new_obj;
  },
  //复制值
  assign(new_obj,old_obj){
    for (const key in old_obj) {
      if (old_obj.hasOwnProperty(key)) {
        const element = old_obj[key];
        if(element){
          new_obj[key]=element;
        }
      }
    }
    return new_obj;
  },
  //点号分离字符串
  dot_filter(resource_info){
    for (const key in resource_info) {
      if (resource_info.hasOwnProperty(key)) {
          const element = resource_info[key];
          if(key.indexOf('.')>-1){
              let arr=key.split(".");
              if(arr.length>0){
                  let last_key:any=arr.pop();
                  resource_info[last_key]=element;
                  delete resource_info[key];
              }
          }
      }
    }
  },
  //md5加密
  md5(string:string){
    return crypto.createHash('md5').update(string).digest('hex');
  },
  //分页处理逻辑
  page_param(param){
      let pageIndex=param["page"] ? param['page'] : 1;
      let pageSize:any=param['page_size'] ? parseInt(param['page_size']) : 10;
      pageSize=pageSize ? pageSize : 10;
      let offset=(pageIndex-1)*parseInt(pageSize);
      //(page_now -1)*page_size
      return {
        offset,
        page_size:pageSize
      };
  },
  //分页结果集合
  page_result:function(result,page_size){
      if(!result || !result.rows){
          if(Array.isArray(result)){
              return {'data_count':0,'page_count':0,'data_info':result}
          }
          return false;
      }
      var data_count=result.count;
      let round=result.count/page_size
      var page_count=round<1 ? 1:Math.round(round) ;
      return {'data_count':data_count,'page_count':page_count,'data_info':result.rows}
  },
  //非空过滤
  not_null_filter:function(param){
    if(typeof param=='object'){
      let result={};
      for (const key in param) {
        if (param.hasOwnProperty(key)) {
          const element = param[key];
          if(element){
            result[key]=element;
          }
        }
      }
      return result;
    }
    return {};
  },
  //修改的结果逻辑
  update_result(update){
      if(update[0]==1){
          return {message:"修改成功",code:200};
      }else if(update[0]==0){
          return {message:"未修改数据",code:200};
      }
      return {message:"操作失败",code:0};
  },
  //时间戳转天
  to_days(time){
    return this.parse_int(time)/(24*3600);
  },
  //格式化时区
  format_time_zone(time,offset){
    let d=new Date(time); //创建一个Date对象 time时间 offset 时区 中国为 8
    let localTime = d.getTime();//获取的是毫秒级
    let wishTime= this.parse_int(localTime) + this.parse_int(3600000*offset);
    return new Date(wishTime);
  },
  //格式化时间
  date_format(date, fmt) {
    //date=this.format_time_zone(date,8);
    if(typeof date=='string'){
      date=new Date(date);
    }
    if (null == date || undefined == date) return '';
    let o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "S": date.getMilliseconds() //毫秒
    };
    
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  //通用的的格式化时间
  date_format_basic(date){
      return this.date_format(date,'yyyy-MM-dd hh:mm:ss');
  },
  //成功结果处理
  success_result(message?:any,data?:any,code?:number){
    
    let result:any={};
    if(!code){
      result.code=200;
    }
    if(message&&message.indexOf(":")>-1){
      result.message=message.split(":").pop();
    }else{
      
      result.message=message?message:"操作成功"
    }
    if(data){
      result.data=data;
    }
    return result
  },
  //错误结果处理
  error_result(message?:any,code?:number){
    code=code ? code : 0;
    if(message){
      if(message.indexOf(":")>-1){
        message=message.split(":").pop();
      }
      return {code,message}
    }
    return {code,message:"操作失败"};
  },
  //首字母大写
  first_char_upper(name:string){
    return name.charAt(0).toUpperCase() + name.slice(1)
  },
  //升序排序
  sort_order(order_condition?){
    
    if(order_condition){
      order_condition.push(['sort','ASC'])
      return order_condition
    }else{
      return [['sort','ASC']]
    }
  }
}